package logic.facade;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import logic.service.CalendarService;
import vo.CalendarVO;
import vo.IValueObject;

/**
 *
 * @author Alexander
 */
public class CalendarFacade extends Facade{

    public CalendarFacade() {
        setService(new CalendarService());
    }
    
/**
 * Retorna true o false si encuentra el id de un VO
 */ 
    @Override
    protected boolean exist(IValueObject vo) {
        return !(super.find(((CalendarVO) vo).getId()) == null);
    }

    public boolean deleteSchedule(CalendarVO calendar) {
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        boolean ret = false;
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            tx = em.getTransaction();
            tx.begin();
            ((CalendarService)getService()).deleteSchedule(calendar, em);
            tx.commit();
            ret = true;
        }catch (Exception e) {
            e.printStackTrace();
            if (em != null && tx != null) {
                tx.rollback();
            }
        } finally {
            if (em != null) {
                em.clear();
                em.close();
            }
            return ret;
        }
    }
    
}
